package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBlacklist;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 黑名单
 */
public class UserBlacklistDaoImpl extends TempDataObjectDao<UserBlacklist> {

	public Map<Integer, Map<Long, UserBlacklist>> getUserBlacklistMap(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer,Map<Long, UserBlacklist>> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_u_blacklist where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserBlacklist data = parse(rs);
				if(!dataMap.containsKey(data.getType())){
					dataMap.put(data.getType(), new ConcurrentHashMap<>());
				}
				dataMap.get(data.getType()).put(data.getBlacklistUserId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Long, Map<Integer, Map<Long, UserBlacklist>>> getAllUserBlacklistMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Long, Map<Integer, Map<Long, UserBlacklist>>> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_u_blacklist";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UserBlacklist data = parse(rs);
				if(!dataMap.containsKey(data.getUserId())){
					dataMap.put(data.getUserId(), new ConcurrentHashMap<>());
				}
				Map<Integer, Map<Long, UserBlacklist>> typeMap = dataMap.get(data.getUserId());
				if(!typeMap.containsKey(data.getType())){
					typeMap.put(data.getType(), new ConcurrentHashMap<>());
				}
				typeMap.get(data.getType()).put(data.getBlacklistUserId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserBlacklist data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_blacklist(userId,blacklistUserId,isDelete,lastAddBlackListTime,type) VALUES(?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setLong(i++, data.getBlacklistUserId());
			call.setBoolean(i++, data.isDelete());
			call.setLong(i++, data.getLastAddBlackListTime());
			call.setInt(i++, data.getType());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserBlacklist data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_blacklist SET isDelete=?,lastAddBlackListTime=? WHERE userId=? and blacklistUserId=? and type=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setBoolean(i++, data.isDelete());
			call.setLong(i++,data.getLastAddBlackListTime());
			call.setLong(i++, data.getUserId());
			call.setLong(i++, data.getBlacklistUserId());
			call.setInt(i++, data.getType());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserBlacklist parseData(ResultSet rs) throws SQLException {
		UserBlacklist data = new UserBlacklist();
		data.setUserId(rs.getLong("userId"));
		data.setBlacklistUserId(rs.getLong("blacklistUserId"));
		data.setDelete(rs.getBoolean("isDelete"));
		data.setLastAddBlackListTime(rs.getLong("lastAddBlackListTime"));
		data.setType(rs.getInt("type"));
		return data;
	}
}
